package com.lang.generator.service.impl;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.lang.generator.common.constant.GlobalConstant;
import com.lang.generator.core.helper.TemplateHelper;
import com.lang.generator.domain.dto.ContentDto;
import com.lang.generator.domain.dto.DataModelParseDto;
import com.lang.generator.domain.dto.TemplateDto;
import com.lang.generator.domain.dto.TemplateResultDto;
import com.lang.generator.domain.enums.TemplateTypeEnum;
import com.lang.generator.service.TemplateService;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipOutputStream;

/**
 * 模板服务impl
 *
 * @author zhangren
 * @date 2022/12/13
 */
@Service
public class TemplateServiceImpl implements TemplateService {

    @Resource
    private TemplateHelper templateHelper;

    @Override
    public List<TemplateDto> getDefaultTemplate(@Nullable TemplateTypeEnum templateTypeEnum) {
        if (Objects.isNull(templateTypeEnum)) {
            templateTypeEnum = TemplateTypeEnum.MVC;
        }
        List<TemplateResultDto> templateList = getTemplateList();
        for (TemplateResultDto templateResultDto : templateList) {
            if (StrUtil.equals(templateResultDto.getTemplateType(), templateTypeEnum.getValue())) {
                return templateResultDto.getTemplateList();
            }
        }
        return Collections.emptyList();
    }

    @Override
    public List<ContentDto> parseTemplate(List<TemplateDto> templateDtoList, List<DataModelParseDto> dataModelList) {
        ArrayList<ContentDto> contentDtoList = new ArrayList<>();
        for (DataModelParseDto dataModel : dataModelList) {
            for (TemplateDto templateDto : templateDtoList) {
                ContentDto contentDto = templateHelper.parseTemplate(templateDto, dataModel);
                contentDtoList.add(contentDto);
            }
        }
        return contentDtoList;
    }

    @Override
    public void appendZipStream(ZipOutputStream zipOutputStream, ContentDto contentDto) {
        templateHelper.appendZipStream(zipOutputStream, contentDto);
    }

    @Override
    public List<TemplateResultDto> getTemplateList() {
        String template = ResourceUtil.readUtf8Str("template.json");
        List<TemplateResultDto> templateResultDtoList = JSONUtil.toList(template, TemplateResultDto.class);
        for (TemplateResultDto templateResultDto : templateResultDtoList) {
            for (TemplateDto templateDto : templateResultDto.getTemplateList()) {
                String templatePath = templateDto.getTemplatePath();
                templateDto.setTemplateContent(getTemplateContent(templatePath));
            }
        }
        return templateResultDtoList;
    }

    private String getTemplateContent(String path) {
        return ResourceUtil.readUtf8Str(GlobalConstant.templatePath + "/" + path);
    }

}
